package com.minidb.engine;

import com.minidb.sql_compliler.ASTNode;

import java.io.IOException;

public class Executor {
    //TODO ：执行器
    private CatalogManager catalogManager;
    private StorageEngine storageEngine;

    //单例模式
    public Executor(){
        this.catalogManager =new CatalogManager();
        this.storageEngine  =new StorageEngine();
    }

    public String execute(ASTNode ast){
        switch (ast.type) {
            case "create_table":
                ASTNode.CreateTableNode ct = (ASTNode.CreateTableNode) ast;
                catalogManager.createTable(ct.tableName, ct.columns);
                return "Table " + ct.tableName + " created.";
            case "insert":
                ASTNode.InsertNode ins = (ASTNode.InsertNode) ast;
                //确认表存在
                catalogManager.getTable(ins.tableName);
                storageEngine.insert(ins.tableName, ins.values);
                return "1 row inserted into " + ins.tableName + ".";
            default:
                return "Unsupported AST node type: " + ast.type;
        }
    }
}
